<template>
    <view>
    </view>
</template>
<script lang="ts">
    import View from 'android.view.View'
    import Context from 'android.content.Context'
	import TextView from 'android.widget.TextView';
    import BufferedReader from 'java.io.BufferedReader'
    import InputStream from 'java.io.InputStream'
    import InputStreamReader from 'java.io.InputStreamReader'
    import Typeface from 'android.graphics.Typeface'
    import Base64 from 'android.util.Base64'
    import ByteArrayInputStream from 'java.io.ByteArrayInputStream'
	import Color from 'android.graphics.Color';
	import LinearLayout from 'android.widget.LinearLayout';
	import ValueAnimator from 'android.animation.ValueAnimator';
	import ObjectAnimator from 'android.animation.ObjectAnimator';
	import TextUtils from 'android.text.TextUtils';
	import Animation from 'android.view.animation.Animation';
	import RotateAnimation from 'android.view.animation.RotateAnimation';
	import remixicon from './remixicon.uts'
	
	
	import Gravity from 'android.view.Gravity';
	import {getUid,getDefaultColor} from "../../../x-core/utssdk/app-android/index.uts"
	let btnFlagId = "center_icon_tag"+getUid();
	class onClickLis extends View.OnClickListener{
		call:()=>void;
		constructor(call:()=>void){
			this.call = call;
		}
		override onClick(view : View){
			this.call();
		}
	}
	/**
	 * @displayName 手势库 xIconU
	 * @version 1.0.0
	 * @author tmzdy
	 * @link 对应PC版本  https://tmui.design/x-icon-u.html 
	 * @deprecated x-icon-u,给你x-icon是一样的，但它属性api版本。
	 * 
	 */
    export default {
        name: "x-icon-u",
        data() {
            return {}
        },
		emits:['click'],
		props:{
			name:{
				type:String,
				default:""
			},
			color:{
				type:String,
				default:""
			},
			fontSize:{
				type:Number,
				default:32
			},
			spin:{
				type:Boolean,
				default:false
			},
			rotation:{
				type:Number,
				default:0
			}
		},
		watch:{
			name(newvalue:string){
				if (this.$el != null) {
					let button = this.$el!.findViewWithTag(btnFlagId) as TextView
					if (!TextUtils.isEmpty(newvalue)) {
						this.setIcon(button,newvalue)
					}
				}
			},
			color(newvalue:string){
				if (this.$el != null) {
					let button = this.$el!.findViewWithTag(btnFlagId) as TextView
					if (!TextUtils.isEmpty(newvalue)) {
						button.setTextColor(Color.parseColor(getDefaultColor(newvalue)));
					}
				}
			},
			fontSize(newvalue:number){
				if (this.$el != null) {
					let button = this.$el!.findViewWithTag(btnFlagId) as TextView
					button.setTextSize((UTSAndroid.rpx2px(newvalue)).toFloat());
				}
			},
			rotation(newvalue:number){
				if (this.$el != null) {
					let button = this.$el!.findViewWithTag(btnFlagId) as TextView
					this.setRotation(button,newvalue)
				}
			},
			spin(newvalue:boolean){
				if (this.$el != null) {
					let button = this.$el!.findViewWithTag(btnFlagId) as TextView
					this.setSpin(button,newvalue)
				}
			}
		},
        NVLoad(): LinearLayout {
			let textView = new TextView(this.$androidContext)
			let layOut = new LinearLayout(this.$androidContext);
			let assetManager = this.$androidContext!.getAssets();
			let typeface = Typeface.createFromAsset(assetManager, "remixicon.ttf")
			let textOpts = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
			textView.setTag(btnFlagId);
			textView.setLayoutParams(textOpts)
			 textView.setTypeface(typeface)
			 textView.setGravity(Gravity.CENTER)
			 
			 this.setIcon(textView,this.name)
			 if (!TextUtils.isEmpty(this.color)) {
			 	textView.setTextColor(Color.parseColor(getDefaultColor(this.color)));
			 }
			 textView.setTextSize((UTSAndroid.rpx2px(this.fontSize)).toFloat());
			 
			 this.setRotation(textView)
			 this.setSpin(textView,this.spin)
			 
			 textView.setOnClickListener(new onClickLis(()=>{
				 this.$emit("click")
			 }))
			 // textView.animate().rotationBy(rotation.toFloat()).setDuration(1000);
			 // .setDuration(1000).setInterpolator(new LinearInterpolator()).setRepeatCount(ValueAnimator.INFINITE).start()
			 layOut.addView(textView)
            return layOut
        },
		methods:{
			setIcon(textView:TextView,code:string){
				if(!TextUtils.isEmpty(code)){
					let codePoint = Integer.parseInt(remixicon.getString(code), 16);
					let charArray = Character.toChars(codePoint);
					let text = new String(charArray);
					textView.setText(text);
				}
				
			},
			setRotation(textView:TextView,rotation:number = 0){
				textView.animate().rotationBy(this.rotation.toFloat()).setDuration(1000)
			},
			setSpin(textView:TextView,start:boolean){
				if(!start){
					textView.clearAnimation();
				}else{
					let rotationStart = 0
					let rotation = 360
					let rotationSFD = 0.5
					let rotationAnimator = ObjectAnimator.ofFloat(textView, "rotation", rotationStart.toFloat(), rotation.toFloat());
					rotationAnimator.setDuration(3000);
					//uts暂时不支持线性动画插值。
					// rotationAnimator.setInterpolator(new LinearInterpolator());
					rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
					rotationAnimator.start();
				}
				
			}
		}
    }
</script>
